import json
import requests
import streamlit as st

# st.title("智能报告生成")
st.title("AI测试报告--智能生成")
# 左侧边栏的布局
sidebar_left = st.sidebar.container()
sidebar_left.title("邮箱配置")
email_user = sidebar_left.text_input(label="登录账户")
email_pass =sidebar_left.text_input(label="密码",type="password",help="注意不是邮箱登录的账户密码！是支持代码调用的那个配置的密码")
email_sender = sidebar_left.text_input(label="收件人邮箱", help="多邮箱之间通过逗号区分")
sidebar_left.title("Ai配置")
ai_api = sidebar_left.text_input(label="appid",help="376a1324xuehaoye")
ai_apiSecret = sidebar_left.text_input(label="apiSecret",help="ZTJiMjMzYTAyMjdhZTMxZTJmNmNjMmY5xuehaoye")
api_Key = sidebar_left.text_input(label="Key",help="ae1f77a1c3cf0c116651dbf91b1d8aa7xuehaoye")
sidebar_left.title("Jira配置")
jira_url =sidebar_left.text_input(label="系统地址")
jira_user =sidebar_left.text_input(label="账户")
jira_pass =sidebar_left.text_input(label="密码",type="password")
# 主页面布局
container1 = st.container()
api_url = container1.text_input(label="接口请求地址",help='/newreporter')
key = container1.text_input(label="问题单Key")
result = container1.radio("测试结果",['测试通过','测试不通过'])
msg = container1.text_area(label="测试结论")
if st.button('生成报告'):
    if not (jira_pass and jira_user and jira_pass and ai_api and ai_apiSecret and api_Key and email_pass and email_sender and email_user and  key and result and msg):
        st.error("请填写完所有参数，再进行生成操作。。")
    else:
        with st.spinner("正在生成中，请耐心等待..."):
            response = requests.request(method="POST", url=api_url, headers={"Content-Type":"application/json"},data=json.dumps({
              "email": {
                "username": email_user,
                "password": email_pass,
                "sender": email_sender},
              "Ai": {
                "appid": ai_api,
                "api_secret": ai_apiSecret,
                "api_key": api_Key},
              "jira": {
                "url": jira_url,
                "username": jira_user,
                "password": jira_pass,
                "key": key},
              "test": {
                "result": result,
                "test": msg}}))
        if response.status_code != 200:
            st.error(f"生成失败，错误原因如下：{response.text}")
        else:
            st.balloons()
            st.success("成功生成报告，可以去邮箱查看生成报告啦")



